package cheme.ProcessModel.Interpreter;

import java.util.Iterator;

import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.IParent;
import cheme.ProcessModel.ModelEnv;

/*
 * Iterates of each field of a tag component
 */
public abstract class ParentIteratingRule implements IRule
{
	protected Interpreter interpreter = null;
	private IParent object = null;

	protected abstract RuleResult tokenCheck(IChild child);

	public RuleResult applyTo(IModelElement object)
	{
		RuleResult result = null;
		
		if(object instanceof IParent)
		{
			setObject((IParent)object);
			interpreter = ModelEnv.getInstanceFor(getObject()).getInterpreter();
			
			Iterator iterator =  getObject().iterator();//.getChildren().iterator();
			IChild child = null;
						
			while(iterator.hasNext() && ((null == result) || (result instanceof IRule.Success)))
			{
				child = (IChild)iterator.next();
				result = tokenCheck(child);
			}
		}
		
		return result;
	}

	/**
	 * @param equation the equation to set
	 */
	public void setObject(IParent object)
	{
		this.object = object;
	}

	/**
	 * @return the equation
	 */
	public IParent getObject()
	{
		return object;
	}
}
